/**
 * Created by lwy on 2017/7/6.
 */

var page=0;
var num=0;

$(function(){
    doAjax();
})

function doAjax(){
    page++;
    $.ajax({
        url:'/flowC/getUserFmsFlowV1.1.json',
        dataType:'json',
        type:'post',
        data:{
            page:page,
            token:getItem('token'),
            companyCode:getItem('companyCode'),
        },
        beforeSend:function(){
            if(page==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('#capital-box'));
            }
        },
        success:function(data){
            if(data.code==-1){
                window.location.href="login.html";
            }
            if(data.code==0){
                mui.toast(data.msg);
            }
            if(data.code==1){
                var data = data.datas;
                if(page==1){
                    $('#capital-box').empty();
                    if(data.length==0){
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../phoneImg/kong.png" alt="暂无数据"></div>').appendTo($('#capital-box'));
                    }
                }
                var capitalHtml = '';
                var map = {},
                    dest = [];
                for(var i = 0; i < data.length; i++){
                    var ai = data[i];
                    if(!map[ai.CreateDate]){
                        dest.push({
                            CreateDate: ai.CreateDate,
                            CreateTime: ai.CreateTime,
                            Change_num:ai.Change_num,
                            Id:ai.Id,
                            ReferCode:ai.ReferCode,
                            Source:ai.Source,
                            data: [ai]
                        });
                        map[ai.CreateDate] = ai;
                    }else{
                        for(var j = 0; j < dest.length; j++){
                            var dj = dest[j];
                            if(dj.CreateDate == ai.CreateDate){
                                dj.data.push(ai);
                                break;
                            }
                        }
                    }
                }
                for(var i=0;i<dest.length;i++){
                    capitalHtml+='<div class="capital-item1 mui-clearfix"><h3 class="capital-item1-time">'+dest[i].CreateDate+'</h3>';
                    for(var a=0;a<dest[i].data.length;a++){
                        capitalHtml+='<div class="mui-clearfix">'+
                            '<div class="capital-item1-left" id="1"><span class="line"></span><b></b><span class="line line2"></span></div>'+
                            '<div class="capital-item1-sanjiao"></div>'+
                            '<div class="capital-item1-right">'+
                            '<span class="time">'+dest[i].data[a].CreateTime+'</span><span class="code">'+dest[i].data[a].ReferCode+'</span>'+
                            '<p class="line3"></p><table>'+
                            '<tr class="one-tr"><td>交易额</td><td>交易类型</td></tr>'+
                            '<tr><td class="money">'+dest[i].data[a].Change_num+'</td><td class="explain">'+dest[i].data[a].Source+'</td></tr>'+
                            '</table></div></div>'
                    }
                    capitalHtml+='</div>';
                }
                $('#capital-box').append(capitalHtml)
                num=data.length;
            }
        }
    })
    return num;
}

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult = doAjax();;
                    if (numResult < 20) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});




////分组排序
//$(function(){
//    $.ajax({
//        url:'/flowC/getUserFmsFlowV1.1.json',
//        dataType:'json',
//        type:'post',
//        data:{
//            token:localStorage.getItem('token'),
//            page:page,
//        },
//        success: function (data) {
//            var data = data.datas;
//
//            var map = {},
//                dest = [];
//            for(var i = 0; i < data.length; i++){
//                var ai = data[i];
//                if(!map[ai.CreateDate]){
//                    dest.push({
//                        CreateDate: ai.CreateDate,
//                        CreateTime: ai.CreateTime,
//                        Change_num:ai.Change_num,
//                        Id:ai.Id,
//                        ReferCode:ai.ReferCode,
//                        Source:ai.Source,
//                        data: [ai]
//                    });
//                    map[ai.CreateDate] = ai;
//                }else{
//                    for(var j = 0; j < dest.length; j++){
//                        var dj = dest[j];
//                        if(dj.CreateDate == ai.CreateDate){
//                            dj.data.push(ai);
//                            break;
//                        }
//                    }
//                }
//            }
//
//            console.log(dest);
//        }
//    })
//})
